package com.fzdy.dataplatform.webutil;

import com.fzdy.dataplatform.entity.Device;
import com.huawei.iotplatform.client.NorthApiClient;
import com.huawei.iotplatform.client.dto.*;
import com.huawei.iotplatform.client.invokeapi.Authentication;
import com.huawei.iotplatform.client.invokeapi.DeviceManagement;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("coapUtil")
@Data
public class CoapUtil {
    @Value("${coap.appId}")
    private String appId;
    @Value("${coap.secret}")
    private String secret;
    @Value("${coap.platformIp}")
    private String platformIp;
    @Value("${coap.platformPort}")
    private String platformPort;
    @Value("${coap.serviceId}")
    private String serviceId;
    @Value("${coap.method}")
    private String method;

    public void registerDevice(String nodeid, String userName, Device device) {
        String deviceId = null;
        try {
            NorthApiClient nac = conn();
            DeviceManagement dm = new DeviceManagement(nac);

            // auth
            Authentication authentication = new Authentication(nac);

            // 4.1.1 鑾峰彇閴存潈Token
            AuthOutDTO aod = authentication.getAuthToken();
            String accessToken = aod.getAccessToken();

            // 4.2.1 娉ㄥ唽鐩磋繛璁惧

            RegDirectDeviceInDTO rddid = new RegDirectDeviceInDTO();

            rddid.setNodeId(nodeid);
            rddid.setVerifyCode(nodeid);
            rddid.setTimeout(0);
            RegDirectDeviceOutDTO rddod = dm.regDirectDevice(rddid, appId, accessToken);

            deviceId = rddod.getDeviceId();
            device.setCoapdeviceid(deviceId);
            device.setCoappsk(rddod.getPsk());
            authentication.logoutAuthToken(accessToken);
            ModifyDeviceInfoInDTO mdiid = new ModifyDeviceInfoInDTO();
            mdiid.setName(nodeid);
            mdiid.setDeviceId(deviceId);
            mdiid.setManufacturerId("Manufacturer001");
            mdiid.setManufacturerName("Manufacturer001");
            mdiid.setDeviceType("WaterMeter");
            mdiid.setModel("NBloTProduct");
            mdiid.setProtocolType("CoAP");
            dm.modifyDeviceInfo(mdiid, appId, accessToken);
            authentication.logoutAuthToken(accessToken);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void delDevice(String deviceId) {
        try {
            NorthApiClient nac = conn();
            DeviceManagement dm = new DeviceManagement(nac);
            // auth
            Authentication authentication = new Authentication(nac);
            AuthOutDTO aod = authentication.getAuthToken();
            // auth
            String accessToken = aod.getAccessToken();
            //4.2.3 删除设备
            dm.deleteDirectDevice(deviceId, appId, accessToken);
            authentication.logoutAuthToken(accessToken);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private NorthApiClient conn() throws Exception {
        NorthApiClient nac = new NorthApiClient();
        ClientInfo ci = new ClientInfo();
        ci.setAppId(appId);
        ci.setPlatformIp(platformIp);
        ci.setPlatformPort(platformPort);
        ci.setSecret(secret);
        nac.setClientInfo(ci);
        nac.initSSLConfig();
        return nac;
    }


}
